package com.szqhyxj.youxiangju.biz.merchantHome.shop.addSite;

import android.text.TextUtils;

import com.szqhyxj.youxiangju.biz.merchantHome.bean.DefaultSite;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.site.SiteModel;
import com.szqhyxj.youxiangju.sys.AbBasePresenter;
import com.szqhyxj.youxiangju.sys.BaseResult;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

class AddSitePresenter extends AbBasePresenter implements AddSiteContract.Presenter{

    private final AddSiteContract.View view;
    private SiteModel.AddressEntity entity;
    public AddSitePresenter(AddSiteContract.View view){
        this.view = view;
        entity = new SiteModel.AddressEntity();
    }

    @Override
    public void init(String addrId){
        this.setAddrId(addrId);
        addDisposables(SiteModel.detailDddress(addrId).observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult<DefaultSite>>(){
                    @Override
                    public void accept(BaseResult<DefaultSite> defaultSiteBaseResult) throws Exception{
                        view.dismissLoading();
                        if(defaultSiteBaseResult.businessLogicSuccess()){
                            view.showDefaultSite(defaultSiteBaseResult.getData());
                        }else {
                            view.showToast(defaultSiteBaseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.dismissLoading();
                        view.showToast(throwable.getMessage());
                    }
                }));
    }


    public void editDddress(){
        if (TextUtils.isEmpty(entity.getPhone())||entity.getPhone().length()<11) {
            view.showToast("请输入正确手机号");
            return;
        }

        if (TextUtils.isEmpty(entity.getUsername())) {
            view.showToast("请输入姓名");
            return;
        }


        if (TextUtils.isEmpty(entity.getCountyCode())) {
            view.showToast("请选择地区");
            return;
        }

        if (TextUtils.isEmpty(entity.getAddress())) {
            view.showToast("请输入详细地址");
            return;
        }


        addDisposables(SiteModel.editDddress(entity)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>(){
                    @Override
                    public void accept(BaseResult baseResult) throws Exception{
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.finishThis();
                            view.showToast("修改成功");
                        }else {
                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showToast(throwable.getMessage());
                        view.dismissLoading();
                    }
                }));
    }

    @Override
    public void presentAddress(){

        if (TextUtils.isEmpty(entity.getPhone())||entity.getPhone().length()<11) {
            view.showToast("请输入正确手机号");
            return;
        }

        if (TextUtils.isEmpty(entity.getUsername())) {
            view.showToast("请输入姓名");
            return;
        }


        if (TextUtils.isEmpty(entity.getCountyCode())) {
            view.showToast("请选择地区");
            return;
        }

        if (TextUtils.isEmpty(entity.getAddress())) {
            view.showToast("请输入详细地址");
            return;
        }


        addDisposables(SiteModel.addAddress(entity)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Consumer<BaseResult>(){
                    @Override
                    public void accept(BaseResult baseResult) throws Exception{
                        view.dismissLoading();
                        if(baseResult.businessLogicSuccess()){
                            view.finishThis();
                        }else {
                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>(){
                    @Override
                    public void accept(Throwable throwable) throws Exception{
                        view.showToast(throwable.getMessage());
                        view.dismissLoading();
                    }
                }));
    }

    @Override
    public void setUsername(String username){
        entity.setUsername(username);
    }

    @Override
    public void setPhone(String phone){
        entity.setPhone(phone);

    }

    @Override
    public void setProvinceCode(String provinceCode){
        entity.setProvinceCode(provinceCode);
    }

    @Override
    public void setCityCode(String cityCode){
        entity.setCityCode(cityCode);
    }

    @Override
    public void setCountyCode(String countyCode){
        entity.setCountyCode(countyCode);
    }

    @Override
    public void setAddress(String address){
        entity.setAddress(address);
    }

    @Override
    public void setAddrId(String addrId){
        entity.setAddrId(addrId);
    }

    @Override
    public void addDisposables(Disposable disposable){
        super.addDisposables(disposable);
        view.showLoading();
    }


}